//
//  CarFriendCell.m
//  LoveCar
//
//  Created by dlios on 15/5/22.
//  Copyright (c) 2015年 薛森鸿. All rights reserved.
//

#import "CarFriendCell.h"
#import <Masonry/Masonry.h>
#import "CarFriendModel.h"
#import "ImgCollectionCell.h"
#import "CarFriendImg.h"
#import "MyCollectionview.h"

@implementation CarFriendCell
- (void)dealloc
{
    [_alabel release];
    [_blabel release];
    [_clabel release];
    [_dlabel release];
    [_headImage release];
    [_imgView release];
    [_collect release];
    [_comment release];
    [_area release];
    [_agreeImg release];
    [_agreeCount release];
    [_commentImg release];
    [_commentCount release];
    [_commentTable release];
    [_array release];
    [_headArray release];
    [_commentArray release];
    [super dealloc];
}
- (void)awakeFromNib {
    [self initAll];
    [self createCollectionView];
}
- (void)initAll
{
    // 属性初始化
    self.dlabel = [[[UILabel alloc] init] autorelease];
    self.area = [[[UILabel alloc] init] autorelease];
    self.agreeCount = [[[UILabel alloc] init] autorelease];
    self.commentCount = [[[UILabel alloc] init] autorelease];
    self.agreeImg = [[[UIImageView alloc] init] autorelease];;
    self.commentImg = [[[UIImageView alloc] init] autorelease];
    self.line = [[[UIView alloc] init] autorelease];
    self.agreePerson = [[[UILabel alloc] init] autorelease];
    self.headArray = [NSMutableArray array];
    self.commentTable = [[[UITableView alloc] init] autorelease];
    self.commentArray = [NSMutableArray array];
    self.array = [NSArray array];
}

#pragma mark 图片

- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 5; // 行间距
    layout.minimumInteritemSpacing = 5; // 列间距
    layout.itemSize = CGSizeMake(80, 80); // item大小
    
    self.collect = [[MyCollectionview alloc] initWithFrame:CGRectMake(0, 0, 10, 1) collectionViewLayout:layout];
    [self.collect registerClass:[ImgCollectionCell class] forCellWithReuseIdentifier:@"cell1"];
    self.collect.backgroundColor = [UIColor clearColor];
    self.collect.dataSource = self;
    self.collect.delegate = self;
    [self.collect release];
    [layout release];
    
    self.comment = [[[UIView alloc] initWithFrame:CGRectMake(62, 400, 280, 220)] autorelease];
    
    UICollectionViewFlowLayout *headlayout = [[UICollectionViewFlowLayout alloc] init];
    headlayout.minimumInteritemSpacing = 5; // 列间距
    headlayout.itemSize = CGSizeMake(30, 30); // item大小
    
    self.headCollect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, 80, 50) collectionViewLayout:headlayout];
    [self.headCollect release];
    [headlayout release];
}

#pragma mark cell数据赋值

- (void)sampleCellWithDic:(NSDictionary *)dic
{
    // 个人信息
    self.alabel.text = [dic objectForKey:@"userName"];
    self.blabel.text = [self intervalSinceNow:[dic objectForKey:@"publishTime"]];
    self.clabel.text = [NSString stringWithFormat:@"来自%@", [dic objectForKey:@"forumName"]];
    _headImage.layer.cornerRadius = 20;
    _headImage.layer.masksToBounds = YES;
    [_headImage sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"userAvatar"]]];
    
    // 消息    
    self.dlabel.textAlignment = UIControlContentVerticalAlignmentTop;
    self.dlabel.textAlignment = 0;
    self.dlabel.numberOfLines = 0;
    self.dlabel.font = [UIFont systemFontOfSize:14];
    self.dlabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_dlabel];
    self.dlabel.text = [NSString stringWithFormat:@"👍%@", [dic objectForKey:@"content"]];
    NSString *str = self.dlabel.text;
    NSDictionary *f = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect rect = [str boundingRectWithSize:CGSizeMake(260, 90) options:NSStringDrawingUsesLineFragmentOrigin attributes:f context:nil];
    
    
#pragma mark 评论部分
    
    // 图片数组
    self.array = [dic objectForKey:@"imageList"];
    //NSLog(@"图片数---%ld", self.array.count);
    _comment.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_comment];
    [self.contentView addSubview:_collect];
    
    if (self.array.count == 0) {
        self.collect.hidden = YES;
        
        self.dlabel.frame = CGRectMake(62, 55, self.frame.size.width - 62 - 20, rect.size.height);
        self.comment.frame = CGRectMake(62, 55 + rect.size.height + 10, self.frame.size.width - 62 - 20, 300);
        
    } else {
        self.collect.hidden = NO;
        
        NSInteger collectHeight = (self.array.count + 2) / 3 * 85;
        
        self.dlabel.frame = CGRectMake(62, 55, self.frame.size.width - 62 - 20, rect.size.height);
        self.collect.frame = CGRectMake(62, 55 + rect.size.height + 10, 250, collectHeight);
        self.comment.frame = CGRectMake(62, self.collect.frame.origin.y + self.collect.frame.size.height + 10, self.frame.size.width - 62 - 20, 300);
    }
    
    [self.collect reloadData];
    
#pragma mark 评论部分
    
    self.commentArray = [dic objectForKey:@"comment"];
    self.headArray = [dic objectForKey:@"agree"];
    
    [_comment addSubview:_area];
    [_comment addSubview:_agreeImg];
    [_comment addSubview:_agreeCount];
    [_comment addSubview:_commentImg];
    [_comment addSubview:_commentCount];
    [_comment addSubview:_line];
    [_comment addSubview:_agreePerson];
    [_comment addSubview:_commentTable];
    [self.comment addSubview:_headCollect];
    
    [self.area mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_comment).with.offset(5);
        make.top.equalTo(_comment).with.offset(5);
        make.width.with.mas_equalTo(@80);
        make.height.with.mas_equalTo(@30);
    }];
    
    _area.text = [dic objectForKey:@"location"];
    _area.font = [UIFont systemFontOfSize:14];
    _area.textColor = [UIColor lightGrayColor];
    
    [self.agreeImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_agreeCount.mas_left).with.offset(-5);
        make.top.equalTo(_comment).with.offset(13);
        make.width.with.mas_equalTo(@15);
        make.height.with.mas_equalTo(@15);
    }];
    [self.agreeImg setImage:[UIImage imageNamed:@"finger"]];
    
    self.agreeCount.frame = CGRectMake(210, 5, 60, 30);
    NSNumber *anum = [dic objectForKey:@"agreeCount"];
    _agreeCount.text = [NSString stringWithFormat:@"%@  |", anum];
    _agreeCount.font = [UIFont systemFontOfSize:14];
    _agreeCount.textColor = [UIColor lightGrayColor];
    [_agreeCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_commentImg.mas_left).with.offset(-4);
        make.top.equalTo(_comment).with.offset(5);
        make.width.with.mas_equalTo(@30);
        make.height.with.mas_equalTo(@30);
    }];
    
    [self.commentImg setImage:[UIImage imageNamed:@"mess"]];
    [_commentImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_commentCount.mas_left).with.offset(-5);
        make.top.equalTo(_comment).with.offset(13);
        make.width.with.mas_equalTo(@15);
        make.height.with.mas_equalTo(@15);
    }];
    
    NSNumber *cnum = [dic objectForKey:@"commentCount"];
    _commentCount.text = [NSString stringWithFormat:@"%@", cnum];
    _commentCount.font = [UIFont systemFontOfSize:14];
    _commentCount.textColor = [UIColor lightGrayColor];
    [_commentCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_comment).with.offset(0);
        make.top.equalTo(_comment).with.offset(5);
        make.width.with.mas_equalTo(@30);
        make.height.with.mas_equalTo(@30);
    }];
    
    self.line.backgroundColor = [UIColor lightGrayColor];
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_comment).with.offset(0);
        make.top.equalTo(_comment).with.offset(35);
        make.width.with.equalTo(_comment);
        make.height.with.mas_equalTo(@1);
    }];
    
    _agreePerson.text = [NSString stringWithFormat:@"等%@人赞过",[dic objectForKey:@"agreeCount"]];
    _agreePerson.font = [UIFont systemFontOfSize:14];
    _agreePerson.textColor = [UIColor lightGrayColor];
    _agreePerson.textAlignment = 2;
    [_agreePerson mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_comment).with.offset(-10);
        make.top.equalTo(_comment).with.offset(40);
        make.width.with.mas_equalTo(@80);
        make.height.with.mas_equalTo(@30);
    }];
    
    self.commentTable.delegate = self;
    self.commentTable.dataSource = self;
    self.commentTable.scrollEnabled = NO;
    self.commentTable.rowHeight = 20;
    self.commentTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.commentTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell3"];
    [self.commentTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_comment).with.offset(0);
        make.top.equalTo(_comment).with.offset(80);
        make.right.equalTo(_comment).with.offset(0);
        make.height.mas_equalTo(@60);
    }];
    [self.commentTable reloadData];
    
    [self.headCollect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell2"];
    self.headCollect.backgroundColor = [UIColor clearColor];
    self.headCollect.dataSource = self;
    self.headCollect.delegate = self;
    
    [self.headCollect reloadData];
    
}

// table协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commentArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:@"cell3"];
    tableCell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = [self.commentArray objectAtIndex:indexPath.row];
    if ([dic objectForKey:@"toUserId"] == 0) {
        NSString *name = [dic objectForKey:@"userName"];
        NSString *content = [dic objectForKey:@"content"];
        tableCell.textLabel.text = [NSString stringWithFormat:@"%@ : %@", name, content];
    }
    if ([dic objectForKey:@"toUserId"] != 0) {
        NSString *name = [dic objectForKey:@"userName"];
        NSString *content = [dic objectForKey:@"content"];
        NSString *toName = [dic objectForKey:@"toUserName"];
        tableCell.textLabel.text = [NSString stringWithFormat:@"%@ 回复 %@ : %@", name, toName, content];
        tableCell.textLabel.font = [UIFont systemFontOfSize:14];
    }
    tableCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return tableCell;
}
// collection协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == self.collect) {
        return self.array.count;
    } else  {
        return self.headArray.count;
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.collect) {
        ImgCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
        [cell.img sd_setImageWithURL:[NSURL URLWithString:[self.array objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"loading.png"]];
        
        return cell;
    }
    if (collectionView == self.headCollect) {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        img.layer.masksToBounds = YES;
        img.layer.cornerRadius = 15;
        NSDictionary *dic = [self.headArray objectAtIndex:indexPath.row];
        [img sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"userAvatar"]]];
        cell.backgroundView = img;
        [img release];
        
        NSInteger collectWidth = 5 * 35;
        self.headCollect.frame = CGRectMake(10, 40, collectWidth, 30);
        return cell;
    }
    
    return nil;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.collect) {
        [self.delegate toCarFriend:self.array index:indexPath.row];
    }
}

// 时间计算
- (NSString *)intervalSinceNow: (NSString *) theDate
{
    NSDateFormatter *date=[[NSDateFormatter alloc] init];
    [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *d=[date dateFromString:theDate];
    
    NSTimeInterval late=[d timeIntervalSince1970]*1;
    
    
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval now=[dat timeIntervalSince1970]*1;
    NSString *timeString=@"";
    
    NSTimeInterval cha=now-late;
    
    if (cha/3600<1) {
        timeString = [NSString stringWithFormat:@"%f", cha/60];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@分钟前", timeString];
        
    }
    if (cha/3600>1&&cha/86400<1) {
        timeString = [NSString stringWithFormat:@"%f", cha/3600];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@小时前", timeString];
    }
    if (cha/86400>1)
    {
        timeString = [NSString stringWithFormat:@"%f", cha/86400];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@天前", timeString];
        
    }
    [date release];
    return timeString;
}

@end
